package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.IdiomXiaoGameEntity;
import com.fenghuajueli.libbasecoreui.listener.OnBaseClick;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 成语消消适配器
 */
public class IdiomXiaoGameAdapter extends BaseAdapter {

    private List<IdiomXiaoGameEntity> idiomXiaoGameEntityList;
    private Context mContext;
    private LayoutInflater mInflater;
    private int itemWidth;
    private OnListClickListener<String> onBaseClick;
    private List<IdiomXiaoGameEntity> alreadySelectList;
    private List<String> idiomList;
    private List<String> idiomRepeatList;
    private OnBaseClick<Integer> onBaseAllRightClick;


    public IdiomXiaoGameAdapter(Context context, List<IdiomXiaoGameEntity> idiomXiaoGameEntityList) {
        this.idiomXiaoGameEntityList = idiomXiaoGameEntityList;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        alreadySelectList = new ArrayList<>();
        itemWidth = (ScreenUtils.getScreenWidth() - ConvertUtils.dp2px(116)) / 8;
    }

    public void refreshData(List<IdiomXiaoGameEntity> idiomXiaoGameEntityList) {
        this.idiomXiaoGameEntityList = idiomXiaoGameEntityList;
        notifyDataSetChanged();
    }

    public void setIdiomList(List<String> idiomList) {
        this.idiomList = idiomList;
    }

    public void setIdiomRepeatList(List<String> idiomRepeatList) {
        this.idiomRepeatList = idiomRepeatList;
    }

    public void setOnBaseClick(OnListClickListener<String> onBaseClick) {
        this.onBaseClick = onBaseClick;
    }

    public void setOnBaseAllRightClick(OnBaseClick<Integer> onBaseAllRightClick) {
        this.onBaseAllRightClick = onBaseAllRightClick;
    }

    @Override
    public int getCount() {
        return idiomXiaoGameEntityList.size();
    }

    @Override
    public Object getItem(int i) {
        return idiomXiaoGameEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.idiom_item_layout, null);
            holder.idiomText = convertView.findViewById(R.id.idiomText);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemWidth, itemWidth);
            convertView.setLayoutParams(layoutParams);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        IdiomXiaoGameEntity xiaoGameEntity = idiomXiaoGameEntityList.get(position);
        holder.idiomText.setText(xiaoGameEntity.getWord());
        if (xiaoGameEntity.getRemove()) {
            holder.idiomText.setVisibility(View.INVISIBLE);
            holder.idiomText.setOnClickListener(null);
        } else {
            holder.idiomText.setVisibility(View.VISIBLE);
            holder.idiomText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (xiaoGameEntity.getSelect()) {
                        xiaoGameEntity.setSelect(false);
                        alreadySelectList.remove(xiaoGameEntity);
                    } else {
                        if (alreadySelectList.size() < 4) {
                            xiaoGameEntity.setSelect(true);
                            alreadySelectList.add(xiaoGameEntity);
                        }
                        //检测四个字的成语是否正确
                        if (alreadySelectList.size() == 4) {
                            if (checkIsRight()) {
                                for (int i = 0; i < alreadySelectList.size(); i++) {
                                    alreadySelectList.get(i).setRemove(true);
                                }
                                alreadySelectList.clear();
                            } else {
                                ToastUtils.showShort("请选择正确的成语关键字哦！");
                            }
                        }
                        if (checkIsAllRight()) {
                            onBaseAllRightClick.onClick(0);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
        }
        if (xiaoGameEntity.getSelect()) {
            holder.idiomText.setBackgroundResource(R.mipmap.aa_grade_bg4);
        } else {
            holder.idiomText.setBackgroundResource(R.mipmap.aa_ci_bg1);
        }
        return convertView;
    }

    private Boolean checkIsAllRight() {
        int count = 0;
        for (int i = 0; i < idiomXiaoGameEntityList.size(); i++) {
            if (idiomXiaoGameEntityList.get(i).getRemove()) {
                count++;
            }
        }
        if (count == idiomXiaoGameEntityList.size()) {
            return true;
        }
        return false;
    }


    private Boolean checkIsRight() {

        boolean isHaveIdiom = false;
        for (int i = 0; i < idiomList.size(); i++) {
            int count = 0;
            for (int j = 0; j < alreadySelectList.size(); j++) {
                if (idiomList.get(i).contains(alreadySelectList.get(j).getWord())) {
                    count++;
                }
            }
            if (count == 4) {
                isHaveIdiom = true;
                setResult();
                break;
            }
        }
        for (int i = 0; i < idiomRepeatList.size(); i++) {
            int count = 0;
            for (int j = 0; j < alreadySelectList.size(); j++) {
                if (idiomRepeatList.get(i).contains(alreadySelectList.get(j).getWord())) {
                    count++;
                }
            }
            if (count == 4) {
                isHaveIdiom = true;
                setResult();
                break;
            }
        }
        return isHaveIdiom;
    }


    private void setResult() {
        String word = "";
        Collections.sort(alreadySelectList, new Comparator<IdiomXiaoGameEntity>() {
            @Override
            public int compare(IdiomXiaoGameEntity xiao1, IdiomXiaoGameEntity t1) {
                int diff = xiao1.getPosition() - t1.getPosition();
                if (diff > 0) {
                    return 1;
                } else if (diff < 0) {
                    return -1;
                }
                return 0;
            }
        });
        for (int j = 0; j < alreadySelectList.size(); j++) {
            word += alreadySelectList.get(j).getWord();
        }
        onBaseClick.itemClick(0, word);
    }

    final class ViewHolder {
        TextView idiomText;
    }

}
